package xiaodou.core.controls
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	import flash.utils.setTimeout;
	
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;
	
	import spark.components.Label;

	public class Toast extends Label
	{
		private static var _toastDic:Dictionary = new Dictionary();
		public static function show(content:String, duration:Number = 2000, key:* = null):Toast
		{
			if(key == null)
				key = content;
			if(_toastDic[key])
				hide(key);
			var toast:Toast = new Toast();
			toast.show(content);
			_toastDic[key] = toast;
			if(duration > 0)
				setTimeout(hide, duration, key);
			return toast;
		}
		
		public static function hide(key:*):void
		{
			if(_toastDic[key])
				(_toastDic[key] as Toast).hide();
			delete _toastDic[key];
		}
		
		public function Toast()
		{
			
		}
		
		/**
		 * 显示 
		 * @param content
		 * @param duration 持续的毫秒数
		 * 
		 */		
		internal function show(content:String):void
		{
			text = content;
			PopUpManager.addPopUp(this, FlexGlobals.topLevelApplication as DisplayObject);
			x = (stage.width - width) / 2;
			y = -height;
			TweenLite.to(this, 0.3, {"y":0});
		}
		
		internal function hide():void
		{
			TweenLite.to(this, 0.3, {"y":-height, "onComplete":hideComplete});
		}
		
		internal function hideComplete():void
		{
			PopUpManager.removePopUp(this);
		}
	}
}